package com.yss.reportworld.controller;

import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.reportworld.domain.DemandBugInfo;
import org.springframework.validation.annotation.Validated;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.Description;
import com.yss.reportworld.service.IDescriptionService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 报表世界说明信息Controller
 *
 * @author yss
 * @date 2023-06-05
 */
@RestController
@RequestMapping("/description")
public class DescriptionController extends BaseController {
    @Autowired
    private IDescriptionService descriptionService;

    /**
     * 查询报表世界说明信息列表
     */
    @RequiresPermissions("reportworld:description:list")
    @GetMapping("/list")
    public TableDataInfo list(Description description) {
        startPage();
        List<Description> list = descriptionService.selectDescriptionList(description);
        return getDataTable(list);
    }

    /**
     * 导出报表世界说明信息列表
     */
    @RequiresPermissions("reportworld:description:export")
    @Log(title = "报表世界说明信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Description description) {
        List<Description> list = descriptionService.selectDescriptionList(description);
        EasyExeclUtil.export(response, list, DemandBugInfo.class);
    }

    /**
     * 获取报表世界说明信息详细信息
     */
    @RequiresPermissions("reportworld:description:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(descriptionService.getById(id));
    }

    /**
     * 新增报表世界说明信息
     */
    @RequiresPermissions("reportworld:description:add")
    @Log(title = "报表世界说明信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated Description description) {
        if (!descriptionService.checkDescriptionNameUnique(description)) {
            return error("说明信息名称【" + description.getName() + "】已存在");
        }
        description.setId(IdUtils.fastSimpleUUID());
        description.setCreateBy(SecurityUtils.getUsername());
        description.setCreateTime(new Date());
        return toAjax(descriptionService.save(description));
    }

    /**
     * 修改报表世界说明信息
     */
    @RequiresPermissions("reportworld:description:edit")
    @Log(title = "报表世界说明信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated Description description) {
        if (!descriptionService.checkDescriptionNameUnique(description)) {
            return error("说明信息名称【" + description.getName() + "】已存在");
        }
        description.setUpdateTime(new Date());
        description.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(descriptionService.updateById(description));
    }

    /**
     * 删除报表世界说明信息
     */
    @RequiresPermissions("reportworld:description:remove")
    @Log(title = "报表世界说明信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(descriptionService.removeByIds(ids));
    }
}
